/* eslint-disable */
import {TrackEvent} from './Tracker'
;(function() {
  const isObject = function(obj) {
    if (obj === undefined || obj === null) {
      return false
    } else {
      return toString.call(obj) == '[object Object]'
    }
  }
  // 劫持Component
  const _componentProto_ = Component
  Component = function(options) {
    //options.methods内有uniapp注入的事件处理器__e及mpHook
    Object.keys(options.methods).forEach(methodName => {
      if (methodName == '__e') {
        //劫持事件处理器
        eventProxy(options.methods, methodName)
      }
    })
    _componentProto_.apply(this, arguments)
  }

  function eventProxy(methodList, methodName) {
    const _funcProto_ = methodList[methodName]
    methodList[methodName] = function() {
      _funcProto_.apply(this, arguments)

      try {
        if (isObject(arguments[0])) {
          if (Object.keys(arguments[0]).length > 0) {
            //记录触发页面信息
            const pages = getCurrentPages()
            const currentPage = pages[pages.length - 1]
            const currentPageVm = currentPage.$vm
            // const type = arguments[0]['type']
            const currentTarget = arguments[0].currentTarget || {}
            const dataset = currentTarget.dataset || {}
            // console.log(999999, dataset, arguments[0])

            if (currentPageVm.tracker) {
              if (dataset[TrackEvent.click]) {
                currentPageVm.tracker.handleClick(dataset)
              }
            }
          }
        }
      } catch (e) {
        console.error('track proxy error', e)
      }
    }
  }
})()
